<?php namespace app\admin\controller;

use houdunwang\view\View;
use system\model\Category;
use system\model\Tag;

class Article extends Common{
    //动作
    public function index(){
//        获取所有的文章数据
        $articles=(new \system\model\Article())->getAllData(1,0);
//        p($articles);die;
        View::with('articles',$articles);
        return view();
    }

    /**
     * @return mixed
     * 文章添加
     */
    public  function articleadd(){
//        读出可选标签数据
        $tags=(new Tag())->gettags();
         View::with('tags',$tags);
        //读出可选分类
        $catagorys=(new Category())->getclasses();
        View::with('catagorys',$catagorys);
           if(IS_POST){
               $res=(new \system\model\Article())->articleadd();
               if($res['valid']){
                   message($res['message'],u('index'),'success');
               }else{
                   message($res['message'],'back','error');
               }
           }
        return view();
    }
    /**
     * 文章修改
     */
    public  function articleedit(){
       if(!isset($_GET['aid'])){
            header("location:index.php?s=admin/article/index");
       }
        $aid=$_GET['aid'];
        if(IS_POST){
           $res= (new \system\model\Article())->articleedit($aid);
            if($res['valid']){
                message($res['message'],u('index'),'success');
            }else{
                message($res['message'],'back','error');
            }
        }
//        获取旧数据
       $oldarticle= (new \system\model\Article())->getarticle($aid);
        View::with('oldarticle',$oldarticle);
//        p($oldarticle);
        //获取所有的分类
        $categorys=(new Category())->getclasses();
//        p($categorys);
        View::with('categorys',$categorys);
        //获取该文章标签信息
        $tag=(new Tag())->gettags($aid);
//        p($tag);
        //获得一维数组
        foreach($tag as $k=>$v){
             if(is_array($v)){
                 foreach($v as $k=>$v){
                   $tga[]=$v;
                 }
             }
        };
//        p($tga);
        View::with('tga',$tga);
//        p($tag);
//        获取所有的标签
        $tags=(new Tag())->gettags();
        View::with('tags',$tags);
//        p($tags);
        return view();
    }
    /**
     * 文章删除，放入回收站
     */
    public function delToArticle(){
        if(!isset($_GET['aid'])){
            header("location:index.php?s=admin/article/index");
        }
        $aid=$_GET['aid'];
        $res= (new \system\model\Article())->delToArticle($aid);
        if($res['valid']){
            message($res['message'],"index",'success');
        }else{
            message($res['message'],"u('back')",'error');
        }
    }

    /**
     * 文章回收站
     */
    public  function  recycle(){
        $articles=(new \system\model\Article())->getAllData(1,1);
//        p($articles);die;
        View::with('articles',$articles);
        return view();
    }
    /**
     * 从回收站恢复
     *
     */
    public  function backTo(){
//        echo 111;
        if(!isset($_GET['aid'])){
            header('location:index.php?s=admin/article/index');
        }
        $aid=$_GET['aid'];
        $res=(new \system\model\Article())->backTo($aid);
        if($res['valid']){
            message($res['message'],"index",'success');
        }else{
            message($res['message'],"u('back')",'error');
        }
    }
    /**
     * 彻底删除
     */
    public function redel(){
        if(!isset($_GET['aid'])){
            header('location:index.php?s=admin/article/index');
        }
        $aid=$_GET['aid'];
        $path=Db::table('article')->where('aid',$aid)->first();
        $path=$path['thumb'];
        if(file_exists($path)){
           unlink($path);
        }
        Db::table('article')->where('aid',$aid)->delete();
        Db::table('articledata')->where('aid',$aid)->delete();
        message('删除成功','index','success');
    }



    //文件上传
    public function upload(){
        $files = File::upload();
//        p($files);die;
        $path=$files[0]['path'];
        Image::thumb($path, $path, 690,330 , 6);
        echo $path;
    }

    public  function  delimg(){
        $path=$_POST['path'];
        if(file_exists($path)){
            $res=unlink($path);
        }
       echo 1;
    }

}

